package com.xtravel.common.helper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import java.io.*;

public class FileHelper {
    private String mDirPath;

    public FileHelper() {
        mDirPath = Environment.getExternalStorageDirectory().toString();
    }

    public FileHelper(String path) {
        mDirPath = String.format("%s/%s", Environment.getExternalStorageDirectory().toString(), path);
        File dir = new File(mDirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    public boolean save(byte[] bytes, String name) {
        String fullPath = getFileNameFullPath(name);
        File file = new File(fullPath);
        try {
            if (file.exists()) {
                if (file.delete()) {
                    file.createNewFile();
                }
            }
            FileOutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
        } catch (Exception e) {
            Log.d("保存文件", String.format("保存文件{0}失败:{1}", fullPath, e.getMessage()));
            return false;
        }
        return true;
    }

    public boolean saveBitmap(Bitmap bitmap, String name) {
        String fullPath = getFileNameFullPath(name);
        File file = new File(fullPath);
        try {
            if (file.exists()) {
                if (file.delete()) {
                    file.createNewFile();
                }
            }
            FileOutputStream out = new FileOutputStream(file);
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
                out.flush();
                out.close();
            }
        } catch (Exception e) {
            Log.d("保存图片", String.format("删除文件{0}失败:{1}", fullPath, e.getMessage()));
            return false;
        }
        return true;
    }

    public Bitmap openAsBitmap(String name) {
        String fullPath = getFileNameFullPath(name);
        return BitmapFactory.decodeFile(fullPath);
    }

    public boolean delete(String name) {
        String fullPath = getFileNameFullPath(name);
        File file = new File(fullPath);
        try {
            if (file.exists()) {
                return file.delete();
            }
            return true;
        } catch (Exception e) {
            Log.d("删除图片", String.format("删除文件{0}失败:{1}", fullPath, e.getMessage()));
            return false;
        }
    }

    public boolean emptyDirectory() {
        File file = new File(mDirPath);
        if (file.isDirectory()) {
            String[] children = file.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = delete(children[i]);
                if (!success) {
                    return false;
                }
            }
        }
        return true;
    }

    public String getFileNameFullPath(String name) {
        return String.format("%s/%s", mDirPath, name);
    }

    public boolean Exists(String name) {
        File file = new File(getFileNameFullPath(name));
        return file.exists();
    }

    public static boolean remove(String fullpath) {
        File f = new File(fullpath);
        return f.delete();
    }

    public static byte[] readBytes(String fullpath) {
        byte[] buffer = null;
        try {
            File file = new File(fullpath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}